﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCore.Models.ViewModels.File.Admin;
using WebCore.Models.ViewModels.File;
using System.Configuration;
using System.IO;
using WebCore.Models.DataManagers;
using WebCore.App_Start;
using WebCore.Models.DataEntities;

namespace WebCore.Controllers
{
    public class FileController : Controller
    {
        //
        // GET: /File/

        #region Admin

        public ActionResult GetManagerView(string rootName, string rootPath)
        {            
            rootName = rootName ?? ConfigurationManager.AppSettings["File_RootFolder"];
            rootPath = rootPath ?? ConfigurationManager.AppSettings["File_RootFolder"];
            string rootFullPath = Server.MapPath("~/" + rootPath);
            
            _FileManagerViewModel _managerViewModel = new _FileManagerViewModel();
            //_managerViewModel.ClientElement = clientElement;
            //_managerViewModel.Root = FileManager.GetFolder(rootName, rootPath, rootFullPath);

            return View("File/Admin/_FileManager", _managerViewModel);
        }

        public ActionResult GetFileListView(string rootName, string rootPath)
        {
            rootName = rootName ?? ConfigurationManager.AppSettings["File_RootFolder"];
            rootPath = rootPath ?? ConfigurationManager.AppSettings["File_RootFolder"];
            string rootFullPath = Server.MapPath("~/" + rootPath);

            _FileManagerViewModel _managerViewModel = new _FileManagerViewModel();
            //_managerViewModel.ClientElement = clientElement;
            _managerViewModel.Root = FileManager.GetFolder(rootName, rootPath, rootFullPath);

            return View("File/Admin/_FileList", _managerViewModel);
        }

        public ActionResult AddNewFolder(string name, string rootPath)
        {
            string error = string.Empty;
            string rootFullPath = Server.MapPath("~/" + rootPath);
            FileManager.AddFolderName(name, rootFullPath, out error);
            return Content(error);
        }

        public ActionResult DeleteFolder(string rootPath)
        {
            string error = string.Empty;
            string rootFullPath = Server.MapPath("~/" + rootPath);
            FileManager.DeleteFolder(rootFullPath, out error);
            return Content(error);
        }

        public ActionResult DeleteFile(string rootPath, string name)
        {
            string error = string.Empty;
            string rootFullPath = Server.MapPath("~/" + rootPath);
            FileManager.DeleteFile(rootFullPath, name, out error);
            return Content(error);
        }

        public ActionResult RenameFile(string path, string oldName, string newName)
        {
            string error = string.Empty;
            string rootFullPath = Server.MapPath("~/" + path);
            FileManager.RenameFile(rootFullPath, oldName, newName, out error);
            return Content(error);
        }

        public ActionResult RenameFolder(string path, string oldName, string newName)
        {
            string error = string.Empty;
            string rootFullPath = Server.MapPath("~/" + path);
            FileManager.RenameFolder(rootFullPath, oldName, newName, out error);
            return Content(error);
            
        }

        [HttpPost]
        public JsonResult UploadFile(string rootPath)
        {
            try
            {
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    if (hpf.ContentLength == 0)
                        continue;
                    string savedFileName = Path.Combine(Server.MapPath("~/" + rootPath), Path.GetFileName(hpf.FileName));
                    hpf.SaveAs(savedFileName);
                }
                return Json(new { errorMessage = "Success" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { errorMessage = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

    }
}
